
/**
 * AJAX 载入用户相册列表页面
 *
 * @return 	void
 */
function bindUserListPhotos() {
	$(".ajax-list-photos").click(function(){
		$(".slider-item a").removeClass('selected-slider-item');
		$(this).addClass('selected-slider-item').blur();
		loadListPhotos($(this).attr('href'));
		return false;
	});
}

var lightboxHandle = null;

/**
 * AJAX 响应分页链接请求
 *
 * @return void
 */
function bindPagesPhotos() {
	$(".edit-photo-link").click(function(){
		lightboxHandle = addMmLightBox('edit-photo-box', 'Edit Photo: ' + $(this).attr('title'), 1);
		var photoID = $(this).attr('target');
		$("#edit-photo-title").val($(this).attr('title'));
		$("#edito-photo-id").val(photoID);
		$("#edit-photo-desc").val($("#photo-thumb-" + photoID).attr('alt'));
		var categoryID = $(this).attr('name');
		$("#edito-category-id").val(categoryID);
		$("#photo-category-list option").each(function(i){
			if(categoryID == $(this).val()) {
				this.selected = true;
			} else {
				this.selected = false;
			}
		});
		return false;
	});

	$(".delete-photo-link").click(function(){
		if(confirm('Do you want to delete the photo: ' + $(this).attr('title'))) {
			var photoID = $(this).attr('target');
			addMmLightBoxMsg('Wait a moment please, delete photo ...', 0, 0, 1);
			$.getJSON($(this).attr("href"), function(dt){
				if(!dt.result) {
					addMmLightBoxMsg(dt.msg, 1, 1);
				} else {
					$("#photo-box-" + photoID).hide();
					addMmLightBoxMsg(dt.msg, 1);
					window.setTimeout(function(){removeMmLightBoxMsg();}, 2000);
				}
			});
		} else {
			$(this).blur();
		}
		return false;
	});

	$(".pages-link").click(function(){
		loadListPhotos($(this).attr('href'));
		return false;
	});

	$(".pages-list").removeAttr("onchange");
	$(".pages-list").change(function(){
		loadListPhotos($(this).val());
		return false;
	});

	$("#edit-photo-form").submit(function(){
		var photoID = $("#edito-photo-id").val();
		var categoryID = $("#edito-category-id").val();
		var newCategoryID = $("#photo-category-list").val();
		var newTitle = $("#edit-photo-title").val();
		var newDesc = $("#edit-photo-desc").val();
		removeMmLightBox('edit-photo-box', lightboxHandle);
		addMmLightBoxMsg('Wait a moment please, save data ...', 0, 0, 1);
		$.post($(this).attr("action"), {photo_id:photoID, category_id:newCategoryID, title:newTitle, desc:newDesc}, function(dt){
			if(!dt.result) {
				addMmLightBoxMsg(dt.msg, 1, 1);
			} else {
				if($("#photo-thumbs-category").attr("name") == categoryID && categoryID != newCategoryID) {
					$("#photo-box-" + photoID).hide();
				}
				addMmLightBoxMsg(dt.msg, 1);
				window.setTimeout(function(){removeMmLightBoxMsg();}, 2000);
			}
		}, 'json');
		return false;
	});
}

/**
 * 载入图片列表
 *
 * @param string load_url 载入地址
 * @return void
 */
function loadListPhotos(load_url) {
	addMmLightBoxMsg('Wait a moment please, loading data ...', 0, 0, 1);
	$.get(load_url, function(dt){
		$('#user-body-box').height('auto').html(dt);
		reactHeight();
		removeMmLightBoxMsg();
		bindPagesPhotos();
	});
}
